package com.nanyang.tms.service.saleprice.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.saleprice.SaleBaseDiscountAddRequest;
import com.nanyang.tms.api.dto.request.saleprice.SaleQueryRequest;
import com.nanyang.tms.api.dto.request.saleprice.SaleUpdateStateRequest;
import com.nanyang.tms.api.dto.request.saleprice.add.*;
import com.nanyang.tms.api.dto.request.saleprice.update.*;
import com.nanyang.tms.api.dto.response.saleprice.*;
import com.nanyang.tms.api.service.saleprice.SalePriceRpcService;
import com.nanyang.tms.dao.saleprice.SalePriceDao;
import com.nanyang.tms.dao.saleprice.SalePriceDiscountDao;
import com.nanyang.tms.domain.saleprice.*;
import com.nanyang.tms.manager.saleprice.SalePriceManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class SalePriceRpcServiceImpl implements SalePriceRpcService {
    @Autowired
    private SalePriceManager salePriceManager;
    @Autowired
    private SalePriceDao saleDao;
    @Autowired
    private SalePriceDiscountDao salePriceDiscountDao;

    @Override
    public ApiResult<PageBean<SalePageResponse>> listSale(SaleQueryRequest param) {
        ApiResult<PageBean<SalePageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            SalePriceQuery query = new SalePriceQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<SalePriceVO> saleList = salePriceManager.listSale(query);
            PageBean<SalePageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(saleList.getTotalItem());
            BeanUtils.copyListProperties(saleList.getData(), resultList.getData(), SalePageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
    @SystemLog(module =SystemLog.Module.PRICE,template="priceDisable")
    @Override
    public ApiResult<Boolean> disableState(SaleUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            SalePriceDO salePriceDO = new  SalePriceDO();
            BeanUtils.copyProperties(param,salePriceDO);
            salePriceDO.setGmtModifiedPin(param.getPin());
            saleDao.disableState(salePriceDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.PRICE,template="priceEnable")
    @Override
    public ApiResult<Boolean> enableState(SaleUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            SalePriceDO salePriceDO = new  SalePriceDO();
            BeanUtils.copyProperties(param,salePriceDO);
            salePriceDO.setGmtModifiedPin(param.getPin());
            saleDao.enableState(salePriceDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> saveSale(SaleBaseAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<Boolean>();
        try {
            param.requestCheck();
            SaleBaseVO saleBaseVO = new  SaleBaseVO();
            saleBaseVO.setGmtCreatePin(param.getPin());
            saleBaseVO.setIp(param.getIp());

            SalePriceVO salePriceVO=new SalePriceVO();
            BeanUtils.copyProperties(param.getSalePriceAddRequest(),salePriceVO);
            saleBaseVO.setSalePriceVO(salePriceVO);

            SalePartSchemeVO salePartSchemeVO=new SalePartSchemeVO();
            SalePartSchemeAddRequest salePartSchemeAddRequest = param.getSalePartSchemeAddRequest();
            BeanUtils.copyProperties(salePartSchemeAddRequest,salePartSchemeVO);
            List<SalePartAddRequest> salePartAddRequestList = salePartSchemeAddRequest.getSalePartAddRequestList();
            List<SalePartVO> salePartVOList =new ArrayList<>();
            for (SalePartAddRequest salePartAddRequest : salePartAddRequestList) {
                SalePartVO salePartVO=new SalePartVO();
                BeanUtils.copyProperties(salePartAddRequest,salePartVO);
                List<SalePartCountyVO> salePartCountyVOList=new ArrayList<>();
                List<SalePartCountyAddRequest> salePartCountyAddRequestList = salePartAddRequest.getSalePartCountyAddRequestList();
                for (SalePartCountyAddRequest salePartCountyAddRequest : salePartCountyAddRequestList) {
                    SalePartCountyVO salePartCountyVO=new SalePartCountyVO();
                    BeanUtils.copyProperties(salePartCountyAddRequest,salePartCountyVO);
                    List<SalePartCityVO> salePartCityVOList=new ArrayList<>();
                    List<SalePartCityAddRequest> salePartCityAddRequestList = salePartCountyAddRequest.getSalePartCityAddRequestList();
                    if(CollectionUtil.isNotEmpty(salePartCityAddRequestList)){
                        BeanUtils.copyListProperties(salePartCityAddRequestList,salePartCityVOList,SalePartCityVO.class);
                    }
                    salePartCountyVO.setSalePartCityVOList(salePartCityVOList);
                    List<SalePartZipcodeVO> salePartZipcodeVOList=new ArrayList<>();
                    List<SalePartZipcodeAddRequest> salePartZipcodeAddRequestList = salePartCountyAddRequest.getSalePartZipcodeAddRequestList();
                    if(CollectionUtil.isNotEmpty(salePartZipcodeAddRequestList)){
                        BeanUtils.copyListProperties(salePartZipcodeAddRequestList,salePartZipcodeVOList,SalePartZipcodeVO.class);
                    }
                    salePartCountyVO.setSalePartZipcodeVOList(salePartZipcodeVOList);
                    salePartCountyVOList.add(salePartCountyVO);
                }
                salePartVO.setSalePartCountyVOList(salePartCountyVOList);
                salePartVOList.add(salePartVO);
            }
            salePartSchemeVO.setSalePartVOList(salePartVOList);
            saleBaseVO.setSalePartSchemeVO(salePartSchemeVO);

            SaleWeightAddRequest saleWeightAddRequest = param.getSaleWeightAddRequest();
            SaleWeightVO saleWeightVO=new SaleWeightVO();
            BeanUtils.copyProperties(saleWeightAddRequest,saleWeightVO);
            List<SaleWeightRecordVO> saleWeightRecordVOList=new ArrayList<>();
            List<SaleWeightRecordAddRequest> saleWeightRecordAddRequestList = saleWeightAddRequest.getSaleWeightRecordAddRequestList();
            BeanUtils.copyListProperties(saleWeightRecordAddRequestList,saleWeightRecordVOList,SaleWeightRecordVO.class);
            saleWeightVO.setSaleWeightRecordVOList(saleWeightRecordVOList);
            saleBaseVO.setSaleWeightVO(saleWeightVO);

            List<SalePriceRecordAddRequest> salePriceRecordAddRequestList = param.getSalePriceRecordAddRequestList();
            List<SalePriceRecordVO> salePriceRecordVOList=new ArrayList<>();
            BeanUtils.copyListProperties(salePriceRecordAddRequestList,salePriceRecordVOList,SalePriceRecordVO.class);
            saleBaseVO.setSalePriceRecordVOList(salePriceRecordVOList);

            salePriceManager.saveSale(saleBaseVO);

            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> updateSale(SaleBaseUpdateRequest param) {
        ApiResult<Boolean> result = new ApiResult<Boolean>();
        try {
            param.requestCheck();
            SaleBaseVO saleBaseVO = new  SaleBaseVO();
            saleBaseVO.setGmtCreatePin(param.getPin());
            saleBaseVO.setIp(param.getIp());

            SalePriceVO salePriceVO=new SalePriceVO();
            BeanUtils.copyProperties(param.getSalePriceUpdateRequest(),salePriceVO);
            saleBaseVO.setSalePriceVO(salePriceVO);

            SalePartSchemeVO salePartSchemeVO=new SalePartSchemeVO();
            SalePartSchemeUpdateRequest salePartSchemeUpdateRequest = param.getSalePartSchemeUpdateRequest();
            BeanUtils.copyProperties(salePartSchemeUpdateRequest,salePartSchemeVO);
            List<SalePartUpdateRequest> salePartUpdateRequestList = salePartSchemeUpdateRequest.getSalePartUpdateRequestList();
            List<SalePartVO> salePartVOList =new ArrayList<>();
            for (SalePartUpdateRequest salePartUpdateRequest : salePartUpdateRequestList) {
                SalePartVO salePartVO=new SalePartVO();
                BeanUtils.copyProperties(salePartUpdateRequest,salePartVO);
                List<SalePartCountyVO> salePartCountyVOList=new ArrayList<>();
                List<SalePartCountyUpdateRequest> salePartCountyUpdateRequestList = salePartUpdateRequest.getSalePartCountyUpdateRequestList();
                for (SalePartCountyUpdateRequest salePartCountyUpdateRequest : salePartCountyUpdateRequestList) {
                    SalePartCountyVO salePartCountyVO=new SalePartCountyVO();
                    BeanUtils.copyProperties(salePartCountyUpdateRequest,salePartCountyVO);
                    List<SalePartCityVO> salePartCityVOList=new ArrayList<>();
                    List<SalePartCityUpdateRequest> salePartCityUpdateRequestList = salePartCountyUpdateRequest.getSalePartCityUpdateRequestList();
                    if(CollectionUtil.isNotEmpty(salePartCityUpdateRequestList)){
                        BeanUtils.copyListProperties(salePartCityUpdateRequestList,salePartCityVOList,SalePartCityVO.class);
                    }
                    salePartCountyVO.setSalePartCityVOList(salePartCityVOList);
                    List<SalePartZipcodeVO> salePartZipcodeVOList=new ArrayList<>();
                    List<SalePartZipcodeUpdateRequest> salePartZipcodeUpdateRequestList = salePartCountyUpdateRequest.getSalePartZipcodeUpdateRequestList();
                    if(CollectionUtil.isNotEmpty(salePartZipcodeUpdateRequestList)){
                        BeanUtils.copyListProperties(salePartZipcodeUpdateRequestList,salePartZipcodeVOList,SalePartZipcodeVO.class);
                    }
                    salePartCountyVO.setSalePartZipcodeVOList(salePartZipcodeVOList);
                    salePartCountyVOList.add(salePartCountyVO);
                }
                salePartVO.setSalePartCountyVOList(salePartCountyVOList);
                salePartVOList.add(salePartVO);
            }
            salePartSchemeVO.setSalePartVOList(salePartVOList);
            saleBaseVO.setSalePartSchemeVO(salePartSchemeVO);

            SaleWeightUpdateRequest saleWeightUpdateRequest = param.getSaleWeightUpdateRequest();
            SaleWeightVO saleWeightVO=new SaleWeightVO();
            BeanUtils.copyProperties(saleWeightUpdateRequest,saleWeightVO);
            List<SaleWeightRecordVO> saleWeightRecordVOList=new ArrayList<>();
            List<SaleWeightRecordUpdateRequest> saleWeightRecordUpdateRequestList = saleWeightUpdateRequest.getSaleWeightRecordUpdateRequestList();
            BeanUtils.copyListProperties(saleWeightRecordUpdateRequestList,saleWeightRecordVOList,SaleWeightRecordVO.class);
            saleWeightVO.setSaleWeightRecordVOList(saleWeightRecordVOList);
            saleBaseVO.setSaleWeightVO(saleWeightVO);

            List<SalePriceRecordUpdateRequest> salePriceRecordUpdateRequestList = param.getSalePriceRecordUpdateRequestList();
            List<SalePriceRecordVO> salePriceRecordVOList=new ArrayList<>();
            BeanUtils.copyListProperties(salePriceRecordUpdateRequestList,salePriceRecordVOList,SalePriceRecordVO.class);
            saleBaseVO.setSalePriceRecordVOList(salePriceRecordVOList);

            salePriceManager.updateSale(saleBaseVO);

            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<SaleBaseResponse> selectByCode(String code) {
        ApiResult<SaleBaseResponse> result=new ApiResult<>();
        try {
            SaleBaseResponse saleBaseResponse=new SaleBaseResponse();
            SalePriceResponse salePriceResponse=new SalePriceResponse();
            SaleBaseVO saleBaseVO = salePriceManager.selectByCode(code);
            BeanUtils.copyProperties(saleBaseVO.getSalePriceVO(),salePriceResponse);
            saleBaseResponse.setSalePriceResponse(salePriceResponse);

            SalePartSchemeResponse salePartSchemeResponse=new SalePartSchemeResponse();
            SalePartSchemeVO salePartSchemeVO = saleBaseVO.getSalePartSchemeVO();
            BeanUtils.copyProperties(salePartSchemeVO,salePartSchemeResponse);
            List<SalePartResponse> salePartResponseList=new ArrayList<>();
            List<SalePartVO> salePartVOList = salePartSchemeVO.getSalePartVOList();
            salePartVOList.forEach(salePartVO->{
                SalePartResponse salePartResponse=new SalePartResponse();
                BeanUtils.copyProperties(salePartVO,salePartResponse);
                List<SalePartCountyResponse> salePartCountyResponseList=new ArrayList<>();
                List<SalePartCountyVO> salePartCountyVOList = salePartVO.getSalePartCountyVOList();
                salePartCountyVOList.forEach(salePartCountyVO ->{
                    SalePartCountyResponse salePartCountyResponse=new SalePartCountyResponse();
                    BeanUtils.copyProperties(salePartCountyVO,salePartCountyResponse);
                    List<SalePartCityResponse> salePartCityResponseList=new ArrayList<>();
                    List<SalePartCityVO> salePartCityVOList = salePartCountyVO.getSalePartCityVOList();
                    if(CollectionUtil.isNotEmpty(salePartCityVOList)){
                        BeanUtils.copyListProperties(salePartCityVOList,salePartCityResponseList,SalePartCityResponse.class);
                    }
                    List<SalePartZipcodeResponse> salePartZipcodeResponseList=new ArrayList<>();
                    List<SalePartZipcodeVO> salePartZipcodeVOList = salePartCountyVO.getSalePartZipcodeVOList();
                    if(CollectionUtil.isNotEmpty(salePartZipcodeVOList)){
                        BeanUtils.copyListProperties(salePartZipcodeVOList,salePartZipcodeResponseList,SalePartZipcodeResponse.class);
                    }
                    salePartCountyResponse.setSalePartZipcodeResponseList(salePartZipcodeResponseList);
                    salePartCountyResponse.setSalePartCityResponseList(salePartCityResponseList);
                    salePartCountyResponseList.add(salePartCountyResponse);
                });
                salePartResponse.setSalePartCountyResponseList(salePartCountyResponseList);
                salePartResponseList.add(salePartResponse);
            });
            salePartSchemeResponse.setSalePartResponseList(salePartResponseList);
            saleBaseResponse.setSalePartSchemeResponse(salePartSchemeResponse);

            SaleWeightResponse saleWeightResponse=new SaleWeightResponse();
            SaleWeightVO saleWeightVO = saleBaseVO.getSaleWeightVO();
            BeanUtils.copyProperties(saleWeightVO,saleWeightResponse);
            List<SaleWeightRecordResponse> saleWeightRecordResponseList=new ArrayList<>();
            List<SaleWeightRecordVO> saleWeightRecordVOList = saleWeightVO.getSaleWeightRecordVOList();
            BeanUtils.copyListProperties(saleWeightRecordVOList,saleWeightRecordResponseList,SaleWeightRecordResponse.class);
            saleWeightResponse.setSaleWeightRecordResponseList(saleWeightRecordResponseList);
            saleBaseResponse.setSaleWeightResponse(saleWeightResponse);

            List<SalePriceRecordResponse> salePriceRecordResponseList=new ArrayList<>();
            BeanUtils.copyListProperties(saleBaseVO.getSalePriceRecordVOList(),salePriceRecordResponseList,SalePriceRecordResponse.class);
            saleBaseResponse.setSalePriceRecordResponseList(salePriceRecordResponseList);

            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(saleBaseResponse);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> setUpDiscount(SaleBaseDiscountAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<Boolean>();
        try {
            param.requestCheck();
            SaleBaseDiscountVO saleBaseDiscountVO = new  SaleBaseDiscountVO();
            BeanUtils.copyProperties(param,saleBaseDiscountVO);
            saleBaseDiscountVO.setGmtCreatePin(param.getPin());
            saleBaseDiscountVO.setIp(param.getIp());
            List<SalePriceDiscountVO> salePriceDiscountVOList=new ArrayList<>();
            BeanUtils.copyListProperties(param.getSalePriceDiscountRequestList(),salePriceDiscountVOList,SalePriceDiscountVO.class);
            saleBaseDiscountVO.setSalePriceDiscountVOList(salePriceDiscountVOList);
            salePriceManager.setUpDiscount(saleBaseDiscountVO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<List<SalePriceDiscountResponse>> querySalePriceDiscount(String saleProductCode) {
        ApiResult<List<SalePriceDiscountResponse>> result=new ApiResult<>();
        try {
            List<SalePriceDiscountResponse> discountList=new ArrayList<>();
            List<SalePriceDiscountVO> salePriceDiscountVOS = salePriceDiscountDao.selectByCode(saleProductCode);
            BeanUtils.copyListProperties(salePriceDiscountVOS,discountList,SalePriceDiscountResponse.class);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(discountList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
